package com.example.attendancesystem_1_8;

//老板设定考勤时间的界面


import android.app.AlertDialog;
import android.content.DialogInterface;
import android.content.Intent;
import android.os.Bundle;

import androidx.fragment.app.Fragment;

import android.os.SystemClock;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.ListView;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ListView;
import android.widget.SimpleAdapter;
import android.widget.Toast;

import com.example.attendancesystem_1_8.Leader.Add_DaKa;
import com.example.attendancesystem_1_8.entity.DakaTime;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * A simple {@link Fragment} subclass.
 * Use the {@link SetTimeFragment#newInstance} factory method to
 * create an instance of this fragment.
 */
public class SetTimeFragment extends Fragment {

    // TODO: Rename parameter arguments, choose names that match
    // the fragment initialization parameters, e.g. ARG_ITEM_NUMBER
    private static final String ARG_PARAM1 = "param1";
    private static final String ARG_PARAM2 = "param2";

    // TODO: Rename and change types of parameters
    private String mParam1;
    private String mParam2;

    private ListView listView;
    //shi pei qi
    private MyListAdapter myListAdapter;

    //setTime Button
    private Button btn_setTime;
    //image
    private int pictureUrl = R.drawable.picture1;
    //List
    private List<DakaTime> list_setTime = new ArrayList<DakaTime>();


    public SetTimeFragment() {
        // Required empty public constructor
    }

    /**
     * Use this factory method to create a new instance of
     * this fragment using the provided parameters.
     *
     * @param param1 Parameter 1.
     * @param param2 Parameter 2.
     * @return A new instance of fragment SetTimeFragment.
     */
    // TODO: Rename and change types and number of parameters
    public static SetTimeFragment newInstance(String param1, String param2) {
        SetTimeFragment fragment = new SetTimeFragment();
        Bundle args = new Bundle();
        args.putString(ARG_PARAM1, param1);
        args.putString(ARG_PARAM2, param2);
        fragment.setArguments(args);
        return fragment;
    }

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        if (getArguments() != null) {
            mParam1 = getArguments().getString(ARG_PARAM1);
            mParam2 = getArguments().getString(ARG_PARAM2);
        }
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        // Inflate the layout for this fragment
        return inflater.inflate(R.layout.tab05, container, false);
    }

    @Override
    public void onActivityCreated(Bundle savedInstanceState) {
        super.onActivityCreated(savedInstanceState);

        listView = getActivity().findViewById(R.id.lv_setTime);
        btn_setTime = getActivity().findViewById(R.id.btn_addList);

        myListAdapter = new MyListAdapter(list_setTime, getActivity());


        //listView.setAdapter(new MyListAdapter(getActivity()));
        listView.setAdapter(myListAdapter);
        listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                //Toast.makeText(getActivity(), "点击 pos:" + position, Toast.LENGTH_LONG).show();
                //SystemClock.sleep(500);
                Intent i = new Intent(getActivity(), Add_DaKa.class);

                /*
                ArrayList<String> myData = new ArrayList<>();
                myData = i.getStringArrayListExtra("outData");
                String workday = new String();
                workday = null;
                for(int j = 3; j < myData.size(); j++)
                {
                    workday = workday + myData.get(j).toString();
                }
                DakaTime dakaTime = new DakaTime(Integer.parseInt(myData.get(0)), Integer.parseInt(myData.get(1)), Integer.parseInt(myData.get(2)), 123, workday, "上班", pictureUrl);

                 */
                startActivity(i);
            }
        });
        listView.setOnItemLongClickListener(new AdapterView.OnItemLongClickListener() {
            @Override
            public boolean onItemLongClick(AdapterView<?> parent, View view, int position, long id) {
                //Toast.makeText(getActivity(), "长按 pos:" + position, Toast.LENGTH_LONG).show();
                //SystemClock.sleep(500);
                final int instead = position;
                DialogInterface.OnClickListener click1=new DialogInterface.OnClickListener()
                {
                    @Override
                    public void onClick(DialogInterface arg0,int arg1)
                    {
                        //android.os.Process.killProcess(android.os.Process.myPid());
                        list_setTime.remove(instead);
                        myListAdapter.notifyDataSetChanged();
                    }
                };
                DialogInterface.OnClickListener click2=new DialogInterface.OnClickListener()
                {
                    @Override

                    public void onClick(DialogInterface arg0, int arg1)
                    {
                        arg0.cancel();
                    }
                };

                int max = list_setTime.size();
                //list_setTime.remove(position);
                //myListAdapter.notifyDataSetChanged();
                AlertDialog.Builder alertdialogbuilder=new AlertDialog.Builder(getActivity());
                alertdialogbuilder.setMessage("是否要删除该打卡项？");
                alertdialogbuilder.setPositiveButton("确定", click1);
                alertdialogbuilder.setNegativeButton("取消", click2);
                AlertDialog alertdialog1=alertdialogbuilder.create();
                alertdialog1.show();

                return true;
            }
        });

        //add list
        btn_setTime.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                DakaTime dakaTime = new DakaTime(9, 0, 30, 12306, "12345", "上班", pictureUrl);
                list_setTime.add(dakaTime);
                myListAdapter.notifyDataSetChanged();
            }
        });
    }

}

